//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "UI/Widgets/Widget.h"

namespace Engine
{
namespace UI
{

	//
	// UI Frame
	//

	class Frame : public Widget
	{
		friend struct ::MetaData::FrameCfg;

	// variables
	protected:
		ScreenPtr		_screen;
		UIAction		_onClose;
		vec2			_designResolution;
		color4u			_color;
		UIMaterial		_material;
		float			_designDips;
		EMeasure::type	_designMeasure;
		bool			_overlay;				// true if Frame is fullscreen and opaque
		bool			_exclusiveInput;


	// methods
	public:
		Frame (EWidget::type wgtType, const EngineSubSystemsRef ess);
		~Frame ();
		
		
		bool IsOverlay ()			const			{ return _overlay; }

		bool IsExclusiveInput ()	const			{ return _exclusiveInput; }

		void SetBackgroundColor (const color4u &c)	{ _color = c; }

		void ReArrange ();

		void OnClose ();

		vec2 const &	GetResolution ()	const	{ return _designResolution; }
		

		// Widget //
		virtual bool ProcessMessage (const WidgetMsg &msg) override;


		static FramePtr New (const EngineSubSystemsRef ess);
	};


	
/*
=================================================
	constructor
=================================================
*/
	inline Frame::Frame (EWidget::type wgtType, const EngineSubSystemsRef ess) :
		Widget(wgtType, ess), _overlay(true), _designResolution(1.0f),
		_designDips(1.0f), _designMeasure(EMeasure::AUTO), _exclusiveInput(true)
	{}
		
/*
=================================================
	destructor
=================================================
*/
	inline Frame::~Frame ()
	{}
	
/*
=================================================
	ReArrange
=================================================
*/
	inline void Frame::ReArrange ()
	{
		typedef WidgetMsg	WM;

		ArrangeHelper	helper;
			
		_dockRegion = _region = rect( -1.0f, -1.0f, 1.0f, 1.0f );

		_visibleForLayout	= true;
		_visible			= true;
		_enabled			= true;
		_stateChanged		= true;

		if ( _designMeasure == EMeasure::AUTO )
			_designMeasure = EMeasure::DEFAULT;

		helper.Setup( ESS()->GetGraphicsEngine()->GetViewport(), _designDips, _designResolution, _region, _designMeasure );

		_SendToAllAttached( WM( WM::ARRANGE, WM::Arrange( helper ) ) );
	}
	
/*
=================================================
	OnClose
=================================================
*/
	inline void Frame::OnClose ()
	{
		_onClose.Call( this );
	}
	
/*
=================================================
	ProcessMessage
=================================================
*/
	inline bool Frame::ProcessMessage (const WidgetMsg &msg)
	{
		typedef WidgetMsg	WM;

		switch ( msg.GetType() )
		{
			case WM::DRAW : {
				if ( _material.IsValid() )
				{
					Ptr< Graphics::BatchRender >	br = ESS()->GetGraphicsEngine()->GetBatchRender().ptr();

					br->AddBatch( _material, Primitives::Rect2D( _region, rect(), _color ) );
				}
				break;
			}

			case WM::ARRANGE : {
				ReArrange();
				return true;
			}

			case WM::SET_COLOR_STYLE : {
				WM::SetColorStyle const &	scs = msg.To< WM::SetColorStyle >();

				if ( scs.target == WM::SetColorStyle::BACKGROUND ) {
					_color = scs.cs.Idle();
					return true;
				}
				return false;
			}
		}

		return Widget::ProcessMessage( msg );
	}

/*
=================================================
	New
=================================================
*/
	inline FramePtr Frame::New (const EngineSubSystemsRef ess)
	{
		return BaseObject::_New( new Frame( EWidget::FRAME, ess ) );
	}


}	// UI
}	// Engine